package com.sykj.why.util;



import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @Author: zjw
 * @Date: 2017/2/13 11:02
 * @Description: 时间工具类
 */
public class DateTool {
    /**
     * 日期格式
     */
    public static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ss";

    public static final String DATEFORMAT_NO_SECOND = "yyyy-MM-dd";

    /**
     * 获取系统时间
     *
     * @return
     */
    public static Long getSystemTime() {
        return new Date().getTime();
    }

    /**
     * String转Long
     *
     * @param time
     * @return long
     */
    public static Long stringToLong(String time, String dateFormat) {
        Long result = null;
        if (dateFormat == null || "".equals(dateFormat)) {
            dateFormat = DateTool.DATEFORMAT;
        }
        //为空就给当前时间
        if (time == null || "".equals(time)) {
            result = new Date().getTime();
        } else {
            DateFormat formatter = new SimpleDateFormat(dateFormat, Locale.CHINA);
            try {
                result = formatter.parse(time).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static Long stringToLong(String time) {
        Long result = null;
        //为空就给当前时间
        if (time == null || "".equals(time)) {
            result = new Date().getTime();
        } else {
            DateFormat formatter = new SimpleDateFormat(DateTool.DATEFORMAT, Locale.CHINA);
            try {
                result = formatter.parse(time).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * Long转String
     *
     * @param time
     * @return
     */
    public static String longToString(Long time, String dateFormat) {
        String result = null;
        if (time != null) {
            DateFormat formatter = new SimpleDateFormat(dateFormat, Locale.CHINA);
            result = formatter.format(new Date(time));
        }
        return result;
    }

    public static String longToString(Long time) {
        String result = null;
        if (time != null) {
            DateFormat formatter = new SimpleDateFormat(DateTool.DATEFORMAT, Locale.CHINA);
            result = formatter.format(new Date(time));
        }
        return result;
    }

    public static String longToyyyyMMddString(Long time) {
        String result = null;
        if (time != null) {
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
            result = formatter.format(new Date(time));
        }
        return result;
    }

    /**
     * 传入时间的年份
     *
     * @param time
     * @return
     */
    public static String getYear(Long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time));
        int y = calendar.get(Calendar.YEAR);
        return y + "";
    }

    /**
     * 传入时间的年月份
     *
     * @param time
     * @return
     */
    public static String getMonth(Long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time));
        //获取月份，范围（0-11）
        int m = calendar.get(Calendar.MONTH) + 1;
        return m < 10 ? "0" + m : m + "";
    }

    /**
     * @param mss 要转换的毫秒数
     * @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
     * @author wy
     */
    public static String formatDuring(long mss) {
        if (mss < 0) {
            mss = 0 - mss;
        }
        long days = mss / (1000 * 60 * 60 * 24);
        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
//        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
//        long seconds = (mss % (1000 * 60)) / 1000;
        return days + " 天 " + hours + "小时 "/* + minutes + " minutes "
                + seconds + " seconds "*/;
    }



    /**
     * 星期转换成从周一开始
     * @author dyb
     * @return
     */
    public static int USDayOfWeekToCN(Calendar calendar){
        int dayOfWeek_US = calendar.get(Calendar.DAY_OF_WEEK);
        int dayOfWeek_CN = dayOfWeek_US == 1 ? 7: dayOfWeek_US - 1;
        return dayOfWeek_CN;
    }



    public static String getMMdd(long timeMillis){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeMillis);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return month+"."+day;
    }

    /**
     * 获取一天零点时间
     * @author dyb
     * @return
     */
    public static long getDayTimeStart(Calendar calendar){
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取一天结束时间点
     * @author dyb
     * @return
     */
    public static long getDayTimeEnd(Calendar calendar){
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTimeInMillis();
    }


    public static String getYearAndMonth(long time){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int year = calendar.get(Calendar.YEAR) ;
        int month = calendar.get(Calendar.MONTH) + 1;
        return year+"-"+month;
    }

    public static long getMonthFirstDay(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        String first = format.format(c.getTime());
        return stringToLong(first,DATEFORMAT_NO_SECOND);
    }

    public static long getMonthLastDay(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        String last = format.format(ca.getTime());
        last += " 23:59:59";
        return stringToLong(last,DATEFORMAT);
    }

    /**
     * 获取一天的时间点时间
     * @author Eleven
     * @return
     */
    public static long getDayTimeDot(Calendar calendar,int dot){
        calendar.set(Calendar.HOUR_OF_DAY, dot);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis();
    }


}

